using System;
using System.Data;
using System.Data.Common;
using System.Configuration;
using System.Data.SqlClient;

using SIS.ENT;


namespace SIS.DAL
{
    /// <summary>
    /// The UserAccountTableDB class is responsible for interacting with the database to retrieve and store information
    /// about UserAccountTable objects.
    /// </summary>
    public class UserAccountTableDB
    {
        #region "Public Methods"

        /// <summary>
        /// Gets an instance of UserAccountTable from the underlying datasource.
        /// </summary>
        /// <param name="userAccountKey">The unique UserAccountKey of the UserAccountTable in the database.</param>
        /// <returns>An UserAccountTable when the UserAccountKey was found in the database, or null otherwise.</returns>
        public static UserAccountTable GetItem(int userAccountKey)
        {
            UserAccountTable myUserAccountTable = null;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocUserAccountTableSelectSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;
                    myCommand.Parameters.AddWithValue("@userAccountKey", userAccountKey);

                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.Read())
                        {
                            myUserAccountTable = FillDataRecord(myReader);
                        }
                        myReader.Close();
                    }
                    myConnection.Close();
                }
                return myUserAccountTable;
            }
        }

        /// <summary>
        /// Returns a list with UserAccountTable objects.
        /// </summary>
        /// <returns>A generics List with the UserAccountTable objects.</returns>
        public static UserAccountTableList GetList()
        {
            UserAccountTableList tempList = new UserAccountTableList();
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocUserAccountTableSelectList", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.HasRows)
                        {
                            while (myReader.Read())
                            {
                                tempList.Add(FillDataRecord(myReader));
                            }
                        }
                        myReader.Close();
                    }
                }
            }
            return tempList;
        }

        /// <summary>
        /// Returns a list with UserAccountTable objects.
        /// </summary>
        /// <returns>A generics List with the UserAccountTable objects.</returns>
        public static UserAccountTableList GetListByUserAccountTypeCode(short UserAccountTypeCode)
        {
            UserAccountTableList tempList = new UserAccountTableList();
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocUserAccountTableSelectListByUserAccountTypeCode", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;
                    myCommand.Parameters.AddWithValue("@userAccountTypeCode", UserAccountTypeCode);
                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.HasRows)
                        {
                            while (myReader.Read())
                            {
                                tempList.Add(FillDataRecord(myReader));
                            }
                        }
                        myReader.Close();
                    }
                }
                return tempList;
            }
        }

        /// <summary>
        /// Saves a UserAccountTable in the database.
        /// </summary>
        /// <param name="myUserAccountTable">The UserAccountTable instance to save.</param>
        /// <returns>The new UserAccountKey if the UserAccountTable is new in the database or the existing UserAccountKey when an item was updated.</returns>
        public static int Save(UserAccountTable myUserAccountTable)
        {
            int result = 0;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocUserAccountTableInsertUpdateSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    if (myUserAccountTable.UserAccountKey == -1)
                    {
                        myCommand.Parameters.AddWithValue("@userAccountKey", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@userAccountKey", myUserAccountTable.UserAccountKey);
                    }
                    if (myUserAccountTable.SchoolKey == -1)
                    {
                        myCommand.Parameters.AddWithValue("@schoolKey", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@schoolKey", myUserAccountTable.SchoolKey);
                    }
                    if (string.IsNullOrEmpty(myUserAccountTable.UserAccountAlternateKey))
                    {
                        myCommand.Parameters.AddWithValue("@userAccountAlternateKey", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@userAccountAlternateKey", myUserAccountTable.UserAccountAlternateKey);
                    }
                    if (string.IsNullOrEmpty(myUserAccountTable.AuthCode))
                    {
                        myCommand.Parameters.AddWithValue("@authCode", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@authCode", myUserAccountTable.AuthCode);
                    }
                    if (string.IsNullOrEmpty(myUserAccountTable.AuthSalt))
                    {
                        myCommand.Parameters.AddWithValue("@authSalt", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@authSalt", myUserAccountTable.AuthSalt);
                    }
                    if (myUserAccountTable.UserAccountTypeCode == null)
                    {
                        myCommand.Parameters.AddWithValue("@userAccountTypeCode", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@userAccountTypeCode", myUserAccountTable.UserAccountTypeCode);
                    }

                    if (string.IsNullOrEmpty(myUserAccountTable.Remark))
                    {
                        myCommand.Parameters.AddWithValue("@remark", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@remark", myUserAccountTable.Remark);
                    }

                    if (myUserAccountTable.Status == null)
                    {
                        myCommand.Parameters.AddWithValue("@status", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@status", myUserAccountTable.Status);
                    }

                    //if (myUserAccountTable.CreatedDateTime == null)
                    //{
                    //    myCommand.Parameters.AddWithValue("@createdDateTime", DBNull.Value);
                    //}
                    //else
                    //{
                    //    myCommand.Parameters.AddWithValue("@createdDateTime", myUserAccountTable.CreatedDateTime);
                    //}
                    //if (myUserAccountTable.ModifiedDateTime == null)
                    //{
                    //    myCommand.Parameters.AddWithValue("@modifiedDateTime", DBNull.Value);
                    //}
                    //else
                    //{
                    //    myCommand.Parameters.AddWithValue("@modifiedDateTime", myUserAccountTable.ModifiedDateTime);
                    //}

                    DbParameter returnValue;
                    returnValue = myCommand.CreateParameter();
                    returnValue.Direction = ParameterDirection.ReturnValue;
                    myCommand.Parameters.Add(returnValue);

                    myConnection.Open();
                    myCommand.ExecuteNonQuery();
                    result = Convert.ToInt32(returnValue.Value);
                    myConnection.Close();
                }
            }
            return result;
        }

        /// <summary>
        /// Deletes a UserAccountTable from the database.
        /// </summary>
        /// <param name="userAccountKey">The UserAccountKey of the UserAccountTable to delete.</param>
        /// <returns>Returns true when the object was deleted successfully, or false otherwise.</returns>
        public static bool Delete(int userAccountKey)
        {
            int result = 0;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocUserAccountTableDeleteSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    myCommand.Parameters.AddWithValue("@userAccountKey", userAccountKey);
                    myConnection.Open();
                    result = myCommand.ExecuteNonQuery();
                    myConnection.Close();
                }
            }
            return result > 0;
        }

        #endregion

        /// <summary>
        /// Initializes a new instance of the UserAccountTable class and fills it with the data fom the IDataRecord.
        /// </summary>
        private static UserAccountTable FillDataRecord(IDataRecord myDataRecord)
        {
            UserAccountTable myUserAccountTable = new UserAccountTable();
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("UserAccountKey")))
            {
                myUserAccountTable.UserAccountKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("UserAccountKey"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("UserAccountAlternateKey")))
            {
                myUserAccountTable.UserAccountAlternateKey = myDataRecord.GetString(myDataRecord.GetOrdinal("UserAccountAlternateKey"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("AuthCode")))
            {
                myUserAccountTable.AuthCode = myDataRecord.GetString(myDataRecord.GetOrdinal("AuthCode"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("AuthSalt")))
            {
                myUserAccountTable.AuthSalt = myDataRecord.GetString(myDataRecord.GetOrdinal("AuthSalt"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("UserAccountTypeCode")))
            {
                myUserAccountTable.UserAccountTypeCode = myDataRecord.GetInt16(myDataRecord.GetOrdinal("UserAccountTypeCode"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("SchoolKey")))
            {
                myUserAccountTable.SchoolKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("SchoolKey"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Remark")))
            {
                myUserAccountTable.Remark = myDataRecord.GetString(myDataRecord.GetOrdinal("Remark"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Status")))
            {
                myUserAccountTable.Status = myDataRecord.GetInt16(myDataRecord.GetOrdinal("Status"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CreatedDateTime")))
            {
                myUserAccountTable.CreatedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("CreatedDateTime"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ModifiedDateTime")))
            {
                myUserAccountTable.ModifiedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("ModifiedDateTime"));
            }
            return myUserAccountTable;
        }
    }

}
